home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 2.3 KB | 85 lines | [TEXT/CWIE] |
- import java.awt.Component;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.net.URL;
- import java.net.MalformedURLException;
-
- public class Misc
- {
- /**
- * Completely loads the Image referenced by the given filename.
- * This will block until the image is loaded.
- * @param filename the path of the Image to load.
- * @param watcher the component to use to load the image.
- * @param if true, the image location is treated as a resource relative to the
- * watcher component's class file; if false the location is treated as an absolute file path.
- * @return the loaded Image, or null if the loading fails.
- */
- public static Image loadImage(String filename, Component watcher, boolean isResource)
- {
- Image image = null;
-
- if (filename != null)
- {
- URL url = null;
-
- if (isResource)
- url = watcher.getClass().getResource(filename);
- else
- {
- try
- {
- url = new URL("file", "", filename);
- }
- catch(MalformedURLException exc)
- {
- exc.printStackTrace();
- }
- }
-
- if (url == null)
- {
- System.err.println("loadImage() could not find \"" + filename + "\"");
- }
- else
- {
- image = watcher.getToolkit().getImage(url);
- if (image == null)
- {
- System.err.println("loadImage() getImage() failed for \"" + filename + "\"");
- }
- else
- {
- MediaTracker tracker = new MediaTracker(watcher);
-
- try
- {
- tracker.addImage(image, 0);
- tracker.waitForID(0);
- }
- catch (InterruptedException e) { System.err.println("loadImage(): " + e); }
- finally
- {
- boolean isError = tracker.isErrorAny();
- if (isError)
- {
- System.err.println("loadImage() failed to load \"" + filename + "\"");
- int flags = tracker.statusAll(true);
-
- boolean loading = 0 != (flags & MediaTracker.LOADING);
- boolean aborted = 0 != (flags & MediaTracker.ABORTED);
- boolean errored = 0 != (flags & MediaTracker.ERRORED);
- boolean complete = 0 != (flags & MediaTracker.COMPLETE);
- System.err.println("loading: " + loading);
- System.err.println("aborted: " + aborted);
- System.err.println("errored: " + errored);
- System.err.println("complete: " + complete);
- }
- }
- }
- }
- }
-
- return image;
- }
- }